//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Bisection.h"
using namespace Cephei::QL::Math::Solvers1d;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Solvers1d::CBisection::CBisection (boost::shared_ptr<QuantLib::Bisection>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBisection = NULL;
#endif
	_ppBisection = &childNative;
    
}
Cephei::QL::Math::Solvers1d::CBisection::CBisection (QuantLib::Bisection& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBisection = NULL;
#endif
	_ppBisection = new boost::shared_ptr<QuantLib::Bisection> (&childNative);
    
    _BisectionOwner = owner;
    
}

Cephei::QL::Math::Solvers1d::CBisection::CBisection (CBisection^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBisection = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBisection = new boost::shared_ptr<QuantLib::Bisection> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Solvers1d::CBisection::CBisection (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBisection = NULL;
#endif
	if (!t->IsSubclassOf(CBisection::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Solvers1d::CBisection::CBisection (QuantLib::Handle<QuantLib::Bisection>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBisection = &childNative;
	_ppBisection = &static_cast<boost::shared_ptr<QuantLib::Bisection>>(childNative.currentLink());
    
    _BisectionOwner = owner;
}
Cephei::QL::Math::Solvers1d::CBisection::CBisection (QuantLib::Handle<QuantLib::Bisection> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBisection = &childNative;
	_ppBisection = &static_cast<boost::shared_ptr<QuantLib::Bisection>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Solvers1d::CBisection::CBisection (QuantLib::Bisection childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBisection = NULL;
#endif
	_ppBisection = new boost::shared_ptr<QuantLib::Bisection> (new QuantLib::Bisection (childNative));
    
}
#endif

Cephei::QL::Math::Solvers1d::CBisection::~CBisection ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBisection != NULL)
    {
	    delete _ppBisection;
        _ppBisection = NULL;
    }
}
Cephei::QL::Math::Solvers1d::CBisection::!CBisection ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBisection != NULL)
    {
	    delete _ppBisection;
    }
}
QuantLib::Bisection& Cephei::QL::Math::Solvers1d::CBisection::GetReference ()
{
    if (_ppBisection == NULL) throw REFNEW NativeNullException ();
	return **_ppBisection;
}
boost::shared_ptr<QuantLib::Bisection>& Cephei::QL::Math::Solvers1d::CBisection::GetShared ()
{
    if (_ppBisection == NULL) throw REFNEW NativeNullException ();
	return *_ppBisection;
}
QuantLib::Bisection* Cephei::QL::Math::Solvers1d::CBisection::GetPointer ()
{
    if (_ppBisection == NULL) throw REFNEW NativeNullException ();
	return &**_ppBisection;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Bisection>& Cephei::QL::Math::Solvers1d::CBisection::GetHandle ()
{
	if (_phBisection == NULL)
	{
		_phBisection = new Handle<QuantLib::Bisection> (*_ppBisection);
	}
	return *_phBisection;
}
#endif
bool Cephei::QL::Math::Solvers1d::CBisection::HasNative () 
{
	return (_ppBisection != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

